<?php
session_start();
error_reporting(0);
$data_status = $_POST['data_status'];
if($data_status):

	require_once("function.php");
	
	//上限值 体重*adi
	$user_mail = $_SESSION['login_mail'];
	
	$username = getUserNick($user_mail);
	
	$weight = getUserWeight($username);
	
	$foodNameLimit = getUserEatFoodName($username);
	//print_r($foodNameLimit);
	
	if(empty($foodNameLimit))
	{
		echo "
		<div class=\"score_yourScore score_img cf fl png\"><span>-</span>'</div>
		<div class=\"fl score_adviceP mt80 pb50 ml30\">
			<p class=\"f18 fl dib w\"><span class=\"fl score_img png score_adviceQoute mr10 mt5\"></span>您未填写任何食物，请填写食物信息后再进行打分！</p>
		</div>
		";
		return false;
	}
	
	$LimitData = array();
	
	foreach($foodNameLimit as $key=>$foodName)
	{
		$limitData[] = getLimitValue($weight,$foodName);
	}
	
	foreach($limitData as $keyLimit=>$valueLimit)
	{
		foreach($valueLimit as $keyLimit1=>$valueLimit1)
		{
			$limit[$keyLimit1] = $valueLimit1;
		}
	}
	//print_r($limit);
	//剔除adi值为0的添加剂
 /* 	foreach($LimitData as $keyLimit=>$valueLimit)
	{
		foreach($valueLimit as $keyLimit1=>$valueLimit1)
		{
			//销毁adi为0的添加剂,adi值为0意味着该种添加剂没有上限值
			if($valueLimit1 == 0)
			{
				unset($keyLimit1);
			}
			else
			{
				$limit[$keyLimit1] = $valueLimit1;
			}
		}//end foreach $valueLimit
	}//end foreach $LimitData 
	 */
	//unset($LimitData);
	//print_r($limit);
	//echo "<Hr />";


	//实际值 净含量*最大使用量
	
	//返回净含量和食品名称
	$actualNetcontent = getActualNetcontent($username);
	//print_r($actualNetcontent); 
	
	//返回食品名称和添加剂值
	$actualData = array();
	
	foreach($actualNetcontent as $foodNameValue=>$netcontent)
	{
		$actualData[] = getActualValue($foodNameValue,$netcontent);
	}
	
	//print_r($actualData); 
	//echo "<hr />";
	
	//键名相同的值相加 rebuild a array
	$actualHandle = rebuild_array($actualData);
	
	//删除值为空的数组元素
	foreach($actualHandle as $k=>$v)
	{
		if($v == '')
		{
			unset($actualHandle[$k]);
		}else
		{
			$actual[$k] = $v;
		}
	}
	//print_r($actualHandle);
	//echo "<hr />";
	//print_r($actual); 

	//处理添加剂超标量
	$countData = array();
	foreach($limit as $keyc=>$valuec)
	{
		foreach($actual as $kc=>$vc)
		{	//比较同一种添加剂的上限值和实际值
			if($keyc == $kc)
			{	//实际值>上限值，求出超出比率$overRate
				//$decreaseScore 应该减去的分数
	
				$rateHandle = ((float)$vc / (float)$valuec);
				$overRate = sprintf("%.3f",$rateHandle);
				//echo '<br>';
				switch ($overRate)
				{
					case $overRate < 0.100:
						$decreaseScore = 0;
					break;  
					case $overRate < 0.300:
						$decreaseScore = 1;
					break;
					case $overRate < 0.500:
						$decreaseScore = 2;
					break;
					case $overRate < 0.700:
						$decreaseScore = 4;
					break;
					case $overRate < 0.900:
						$decreaseScore = 4;
					break;
					case $overRate < 1.000:
						$decreaseScore = 5;
					break;
					case $overRate < 1.001:
						$decreaseScore = 6;
					break;
					case $overRate < 1.500:
						$decreaseScore = 7;
					break;
					case $overRate < 1.700:
						$decreaseScore = 9;
					break;
					case $overRate < 1.900:
						$decreaseScore = 10;
					break;
					case $overRate < 2.100:
						$decreaseScore = 11;
					break;
					default:
						$decreaseScore = 12;
				}
				
				$countData[$kc] = $decreaseScore;
				
			}//end if $key = $k
		}//end foreach $actual
	}//end foreach $limit

	
	//unset($actual);
	//print_r($countData);
	//echo "<br />";
	/*
	 * 输出结果及计算分数,按添加剂超过的比率计算分数
	 * $totalScore 预设总分
	 * $additiveNum 添加剂加权后应该减去的分数
	 * $newsNum 添加剂新闻应该减去的分数
	 * $numAdditive 添加剂超标项总数
	 *
	 */
	(int)$numAdditive = 0;
	(float)$additiveNum = 0.0;
	//print_r($countData);
	foreach($countData as $keyn=>$valuen)
	{
		if($valuen > 0)
		{//echo $keyn,$valuen;echo '<br>';
			//添加剂加权后应该减去的分数
			$additiveNum  = $additiveNum + $valuen;
			
			//添加剂超标项总数
			if($valuen >= 6)
			{
				$numAdditive++;
			}
		}
	}//end foreach $countData
	
	//得到新闻中应该减去的分数
	(float)$newsNum = 0.0;
	
	$userCity = getUserCity($username);

	$newsNum = getNewsDecrease($LimitData,$userCity);
	
	unset($limit);
	
	//添加剂最多扣70分
	if($additiveNum > 70)
	{
		$additiveNum = 70;
	}
	
	//新闻最大扣15分
	if($newsNum > 20)
	{
		$newsNum = 15;
	}

	$totalScore = (int)(80 - $additiveNum) + (int)(20 - $newsNum);
	
	//区别年龄
	$user_age = getUserAge($user_mail);

	switch ($user_age)
	{
		case $user_age < 8:
			$totalScore = $totalScore -10;
		break;  
		case $user_age < 18:
			$totalScore = $totalScore - 3;
		break;
		case $user_age < 50:
			$totalScore = $totalScore - 3;
		break;
		case $user_age < 60:
			$totalScore = $totalScore - 5;
		break;
		case $user_age < 80:
			$totalScore = $totalScore - 9;
		break;
		case $user_age < 100:
			$totalScore = $totalScore - 12;
		break;
		default:
			$totalScore = $totalScore - 15;
	}
	
	//modify by zhouyi 2012.7.23 add $netcontent <1000
	//吃食物过多
	foreach($actualNetcontent as $foodNameValue=>$netcontent)
	{
		switch ($netcontent)
		{
			case $netcontent < 500:
				$totalScore = $totalScore -0;
			break;  
			case $netcontent < 1000:
				$totalScore = $totalScore -2;
			break;  
			case $netcontent < 1500:
				$totalScore = $totalScore -5;
			break;
			case $netcontent < 2000:
				$totalScore = $totalScore - 10;
			break;
			case $netcontent < 2500:
				$totalScore = $totalScore - 15;
			break;
			case $netcontent < 3000:
				$totalScore = $totalScore - 20;
			break;
			default:
				$totalScore = $totalScore - 25;
		}
	}
	
	//从新闻标签中减分
	$user_id = getUserId($user_mail);
	$tagFoodArr = descreaseScoreFromTags($user_id);
	//print_r($tagFoodArr);
	foreach($tagFoodArr as $tagKey => $tagValue)
	{
		$temp = 0;
		if( $tagValue > $temp )
		{
			$temp = $tagValue;
		}
	}
	//modify by zhouyi 2012.7.23 change 5 to 1
 	$totalScore = $totalScore - ($temp * 1); 
	//print_r($actualNetcontent);
	
	foreach($actualNetcontent as $foodNameValue=>$netcontent)
	{
		if($netcontent >= 0.5):
		$zl[] = "$foodNameValue";
		endif;
	}

	//总分数不能超过95分
	if($totalScore >= 95)
	{
		$totalScore = 94;
	} 
	
	//总分数不能低于20分
	if($totalScore < 20)
	{
		$totalScore = 21;
	}
	
	
	$str = '';
	foreach($zl as $kzl=>$vzl)
	{
		$str.='<span clss="c91 f14">'.$vzl.'</span><br />';
	}
	$tagFood = '';
	foreach($tagFoodArr as $tagKey => $tagValue)
	{
		$tagFood.='<span class="c91 f14">'.$tagKey.'</span><br />';
	}
	
	if( $numAdditive >= 1 && $str != '' && $tagFood != '' ):
		$tipStrings = '你今日的饮食有<span class="co">'.$numAdditive.'</span>项添加剂超标.<br />
					   下列食物摄入较多：<br><span class="co">'.$str.'</span>
					   下列食物在食品新闻中被涉及:<br /><span class="co f14">'.$tagFood.'</span>
					   <span class="co">请注意饮食！~~~~(>_<)~~~~ </span>';
	elseif($numAdditive >= 1 && $str == '' && $tagFood != '' ):
		$tipStrings = '你今日的饮食有<span class="co">'.$numAdditive.'</span>项添加剂超标.<br />
					   下列食物在食品新闻中被涉及:<br /><span class="co f14">'.$tagFood.'</span>
					   <span>请注意饮食,有利于身体健康！</span>';
	elseif($numAdditive == 0 && $str != '' && $tagFood != '' ):
		$tipStrings = '你今日的饮食有0项添加剂超标.<br />
					   下列食物摄入量过多：<br><span class="co f14">'.$str.'</span>
					   下列食物在食品新闻中被涉及:<br /><span class="co f14">'.$tagFood.'</span>
					   <span class="co">请注意饮食！~~~~(>_<)~~~~ </span>';
	elseif($numAdditive == 0 && $str == '' && $tagFood != ''):
		$tipStrings = '你今日的饮食有0项添加剂超标.<br />
					   下列食物在食品新闻中被涉及:<br /><span class="co f14">'.$tagFood.'</span>
					  </span> 请注意饮食,有利于身体健康！</span>';
		
	elseif( $numAdditive >= 1 && $str != '' && $tagFood == '' ):
		$tipStrings = '你今日的饮食有<span class="co">'.$numAdditive.'</span>项添加剂超标.<br />
		               下列食物摄入量过多：<br><span class="co f14">'.$str.'</span>
					   <span class="co">请注意饮食！~~~~(>_<)~~~~ </span>';
	elseif($numAdditive >= 1 && $str == '' && $tagFood == '' ):
		$tipStrings = '你今日的饮食有<span class="co">'.$numAdditive.'</span>项添加剂超标.<br />
					  <span> 请注意饮食,有利于身体健康！</span>';
	elseif($numAdditive == 0 && $str != '' && $tagFood == '' ):
		$tipStrings = "你今日的饮食有0项添加剂超标.<br />
		               下列食物摄入量过多：<br><span class=\"co f14\">{$str}</span>
					   <span class=\"co\">请注意饮食！~~~~(>_<)~~~~ </span>";
	elseif($numAdditive == 0 && $str == '' && $tagFood == ''):
		$tipStrings = "你今日的饮食有0项添加剂超标.<br />
			           <span>食物量入量较为合理，继续保持！O(∩_∩)O</span>";
	endif;

	//用户历史分数存储
	$user_id = getUserId($user_mail);
	$stringTagFood = '';
	foreach($tagFoodArr as $keytag => $valuetag){
	$stringTagFood = $stringTagFood.$keytag.',';

	}
	//echo $stringTagFood;
	saveUserHistoryScore($user_id,$totalScore,$tipStrings,$numAdditive,$str,$stringTagFood);
		
	echo "
	<div class=\"score_yourScore score_img cf fl png\"><span>{$totalScore}</span>'</div>
	<div class=\"fl score_adviceP mt80 pb50 ml30\">
		<p class=\"f18 fl dib w\"><span class=\"fl score_img png score_adviceQoute mr10 mt5\"></span>{$tipStrings}</p>

		<div class=i\"mt20\">
			<div class=\"home_share fl\"></div>
			<a href=\"report.php\" class=\"tr fr dib score_lookDetails\">查看详情→</a>
		</div>
	</div>
	";

	
endif;//end if $data_status

?>
